<template>
    <div class="contenBox">
        <div class='first'>
          <el-card class="box-card">
            <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="姓名">
                <el-input v-model="maname" placeholder="请输入学生名称"></el-input>
            </el-form-item>
            <el-form-item label="证件号码">
            <el-input v-model="value" placeholder="请输入证件号"></el-input>
          </el-form-item>
            <el-form-item >
              <el-button type="primary" @click="changeSearch" style='width:100px;height:40px;background:#7bb4d0;border:none;'>
                <i class="el-icon-search"></i>
                搜索</el-button>
              <el-button type="success" @click='refreshInput' style='width:100px;height:40px;background:#6ccfcc;border:none;'>
                <i class="el-icon-refresh"></i>
                重置</el-button>
                </el-form-item>
            </el-form>
          </el-card>
         </div>
          <div class="list">
          <h3 style='color:#3c98d9;margin-top:30px;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;管理列表</h3>
          <el-upload class="importBtn" name="file" action="action" :show-file-list="false" :http-request="fileChange">
                <el-button type="primary" style='background:#66c8e3;border:none;'>
                <i  class="iconfont icon-xiazai" style='width:25px;height:25px;'></i>
                导入学生</el-button>
            </el-upload>
          <div style='line-height: 80px;margin-left:10px;'>
              <el-button @click='goaddstudent' type="primary" style='background:#5ca5d2;border:none;'>
                <i class="el-icon-plus"></i>
                新增</el-button>    
                <el-button @click="exportCourse" type="primary" style='background:#fabd90;border:none;'>
               <i  class="iconfont icon-daochu" style='width:20px;height:20px;'></i>
                导出名单</el-button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          </div> 
          
        </div>
        <div style='margin-left:30px;' class="studentTable">
             <template>
                <el-table
                :data="tableData"
                style="width: 94%">
                <el-table-column
                    type='index'
                    label="序号"
                    >
                </el-table-column>
                <el-table-column
                    prop="studentName"
                    label="姓名"
                    itemId="id"
                    width="80">
                </el-table-column>
                <el-table-column
                    prop="gender"
                    :formatter="sfktFormate"
                    label="性别" width="80">
                </el-table-column>
                <el-table-column
                    prop="identificationTypeName"
                    
                    label="证件类型">
                </el-table-column>
                <el-table-column
                    prop="studentIds"
                    label="学号">
                </el-table-column>
                <el-table-column
                    prop="idCardNo"
                    label="证件号码"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="createTime"
                    label="创建时间"
                    width="180">
                </el-table-column>
               <el-table-column
                    prop="suspensionStatus"
                    label="停学状态">
                    <template slot-scope='scope'>
                  <el-switch
                      v-model="scope.row.suspensionStatus"
                      active-color="#13ce66"
                      inactive-color="#C2C2C2"
                      :active-value='1'
                      :inactive-value="0"
                      @change="updateIsUsing(scope.row)"
                    >
                    </el-switch>
                </template>
                </el-table-column>
                <el-table-column
            fixed="right"
            label="操作">
            <template slot-scope="scope">              
                  <span class='icon edit' @click="goeditstudent(scope.$index, scope.row)">编辑</span>
                  <span class='icon del' @click="del(scope.$index, scope.row)">删除</span>
                  <el-button type='text' @click="Checkstudent(scope.$index, scope.row)">查看详情</el-button>      
            </template>
            </el-table-column>
                </el-table>
            </template>
         </div>
            <!-- 分页 -->
    <div class="pagination">
          <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pageNum"
        :page-sizes="[2, 3, 4, 5]"
        :page-size="query.pageSize"
        prev-text="上一页"
        next-text="下一页"
        layout=" prev, pager, next"
        :total="query.total"
      ></el-pagination>

          <div class="jumper">
            跳到第
            <input type="text" ref="pageNum" :value="query.pageNum" />
            页
          </div>
          <button @click="goPage">确定</button>
        </div>
    </div>
</template>

<script>
import {
  studentApi,
  studentEditstatusApi,
  exportApi,
  studentDelApi,
  ImportStudentApi
} from "@/api/educational/student";
export default {
  data() {
    return {
      options: [],
      value: "",
      query: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      id:"",
      maname: "",
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      current: 1,
      filename: "学生管理表"
    };
  },
  mounted() {
    this.studentlist();
  },
  methods: {
    sfktFormate(row) {
      if (row.gender == "1") {
        return "女";
      } else if (row.gender == "0") {
        return "男";
      }
    },
    // identificationType(row) {
    //   if (row.identificationType == "0") {
    //     return "居民身份证";
    //   }if (row.identificationType == "1") {
    //     return "居住证";
    //   }if (row.identificationType == "2") {
    //     return "签证";
    //   }if (row.identificationType == "3") {
    //     return "护照";
    //   }if (row.identificationType == "4") {
    //     return "户口本";
    //   }if (row.identificationType == "5") {
    //     return "港版澳通行证";
    //   }
    // },
    async studentlist() {
      let res = await studentApi(this.query);
      this.tableData = res.rows;
      this.options = res.rows;
      this.query.total = res.total;
    },
    //搜索
    changeSearch() {
      let obj={
         studentName: this.maname,
         idCardNo: this.value
      }
       studentApi(obj).then(res => {
        console.log("222", res);
        this.tableData = res.rows;
        this.query.total = res.total;
      });
      
    },
    //重置
    refreshInput() {
      this.maname = "";
      this.value = "";
      this.studentlist();
    },
    // 分页
    // 改变每页条数
    handleSizeChange(val) {
      this.query.pageSize = val;
      this.studentlist();
    },
    //切换页数，执行方法
    handleCurrentChange(val) {
      this.query.pageNum = val;
      this.studentlist();
    },
    goPage() {
      let { value } = this.$refs.pageNum;
      this.query.pageNum = Number(value);
      this.studentlist();
    },
    //导出
    exportCourse() {
      this.$confirm("是否导出全部数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          exportApi().then(res => {
            if (!res) return;
            const blob = new Blob([res], {
              type: "application/vnd.ms-excel"
            }); // 构造一个blob对象来处理数据，并设置文件类型
            if (window.navigator.msSaveOrOpenBlob) {
              //兼容IE10
              navigator.msSaveBlob(blob, this.filename);
            } else {
              const href = URL.createObjectURL(blob); //创建新的URL表示指定的blob对象
              const a = document.createElement("a"); //创建a标签
              a.style.display = "none";
              a.href = href; // 指定下载链接
              a.download = this.filename; //指定下载文件名
              a.click(); //触发下载
              URL.revokeObjectURL(a.href); //释放URL对象
            }
          });
        })
        .catch(() => {});
    },
    //导入
    fileChange(files) { 
        let formData = new FormData(); 
        formData.append('file', files.file);
        ImportStudentApi(formData).then((res) => {
          console.log(res);
          if(res.code === 200) {
            this.$message({
              type: 'success',
              message: '导入成功',
            });
            this.studentlist();
          }else {
            this.$message({
              type: 'error',
              message: '导入失败',
            });
          }
        });
    },
    //跳转
    goaddstudent() {
      this.$router.push("/educational/addstudent");
    },
    goeditstudent(index,row){
      console.log('33',row)
      this.$router.push({
        name: "Editstudent",
        params: {
          id: row.id
        }    
      });
      localStorage.setItem('id',row.id)
    },
    async Checkstudent(index,row) {
      console.log('33',row)
      this.$router.push({
        name: "CheckStudent",
         params: {
          id: row.id
        }  
      });
      localStorage.setItem('id',row.id)
    },
    //删除
    del(index, row) {
      console.log(row);
      this.$confirm("确认删除记录吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          studentDelApi({ ids: JSON.stringify(row.id) }).then(res => {
            console.log(res);
            this.$message.success("删除成功");
            this.studentlist();
          });
        })
        .catch(() => {});
    },
    //修改状态
    async updateIsUsing(row) {
      console.log(row);
      let obj={
        suspensionStatus: row.suspensionStatus ? 1 : 0,
        id: row.id
      }
     let res=await studentEditstatusApi(obj);
     console.log('234',res)
     this.studentlist();
      this.$message.success("修改成功");
    }
  }
};
</script>
<style lang='scss' scoped >
.contenBox {
  width: 104%;
  height: 100%;
  background-color: #f3f2f7;
  box-sizing: border-box;
}
  .importBtn{
    position: relative;
    top: 20px;
    left: 320px;
  }
.first {
  width: 94%;
}
.box-card {
  width: 99%;
  margin-left: 20px;
  .demo-form-inline {
    display: flex;
    justify-content: left;
  }
}
.list {
  width: 93%;
  margin-left: 20px;
  margin-top: 20px;
  background: #ffffff;
  display: flex;
  justify-content: space-between;
}
.operation {
  display: flex;
  .el-icon-edit-outline {
    color: #94ccea;
    font-size: 15px;
    margin: 12px 0px;
    font-weight: bold;
  }
  .el-icon-delete {
    color: #f75572;
    font-size: 15px;
    font-weight: bold;
    margin: 12px 8px;
  }
}
.studentTable .edit,.del{
  color: #1990FF;
  margin-right: 10px;
  cursor: pointer;
}
.pagination {
  display: flex;
  align-items: center;
  justify-content: right;
  margin-top: 20px;
  margin-right: 150px;
  .jumper {
    font-size: 14px;
    margin-left: 20px;
    margin-right: 10px;
    display: flex;
    align-items: center;
    input {
      width: 40px;
      height: 26px;
      margin: 0 5px;
      text-align: center;
      border: solid 1px #e6e6e6;
      border-radius: 5px;
      color: #666;
      outline-style: none;
    }
  }
  button {
    height: 28px;
    line-height: 28px;
    border-radius: 5px;
    background-color: #4895db;
    border: solid 1px #4895db;
    color: #fff;
    font-size: 14px;
    padding: 0 8px;
  }
}
::v-deep .el-dialog {
  width: 700px;
  height: 500px;
  border-radius: 10px;
  font-weight: 600;
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .message {
    position: absolute;
    left: 40%;
    top: 50%;
    width: 140px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    color: #fff;
    border-radius: 10px;
    background-color: rgba(0, 0, 0, 0.4);
  }
  .fade-enter, 
            .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
  .el-dialog__header {
    border-radius: 10px 10px 0 0;
    text-align: center;
    background-color: rgb(72, 149, 219);
    .el-dialog__title {
      color: #fff;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #fff;
    }
  }

  .addInput {
    display: flex;
    justify-content: center;
    align-items: center;
    .collegeName {
      width: 70px;
    }
    .el-input {
      width: 300px;
    }
  }
  .addSelect {
    display: flex;
    justify-content: center;
    align-items: center;
    // justify-content: space-around;

    .superiorCollege {
      width: 70px;
    }
    .el-select {
      width: 300px;
    }
  }
  .el-dialog__body {
    height: 240px;
  }
  .el-dialog__footer {
    text-align: center;
    button {
      width: 100px;
      height: 35px;
      border-radius: 16px;
      line-height: 0;
    }
    .cancel {
      border: none;
      color: #848383;
      background-color: #f0f0f0;
    }
    .determine {
      border: none;
      background-color: #4995dc;
    }
  }
}
</style>
